/*
 * @Author: aggfb 714740235@qq.com
 * @Date: 2024-10-01 10:22:46
 * @LastEditors: aggfb 714740235@qq.com
 * @LastEditTime: 2024-10-01 16:44:52
 * @FilePath: \baking_machine\bsp\bap_timer.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "bsp_uart.h"
#include <stdio.h>
#include <string.h>

// 调试串口DMA缓存
#define UART3_TX_RX_SIZE 0xff
uint8_t debug_rx_buff[UART3_TX_RX_SIZE] = {0};
uint8_t debug_tx_buff[UART3_TX_RX_SIZE] = {0};


void bsp_uart3_init(void)
{    
    rcu_periph_clock_enable(RCU_GPIOC);
    rcu_periph_clock_enable(RCU_UART3);
    rcu_periph_clock_enable(RCU_AF);
    rcu_periph_clock_enable(RCU_DMA1);

    // 串口初始化
    gpio_init(GPIOC, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10);       // TX
    gpio_init(GPIOC, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_11); // RX

    usart_deinit(UART3);
    usart_baudrate_set(UART3, 115200U);
    usart_receive_config(UART3, USART_RECEIVE_ENABLE); 
    usart_transmit_config(UART3, USART_TRANSMIT_ENABLE);
    usart_enable(UART3);

    // DMA 初始化
    dma_parameter_struct dma_init_struct;
    dma_deinit(DMA1, DMA_CH4);
    dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
    dma_init_struct.memory_addr = NULL;
    dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
    dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
    dma_init_struct.number = UART3_TX_RX_SIZE;
    dma_init_struct.periph_addr = (uint32_t)(&USART_DATA(UART3));
    dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
    dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
    dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
    dma_init(DMA1, DMA_CH4, &dma_init_struct);
    dma_circulation_disable(DMA1, DMA_CH4);
    dma_memory_to_memory_disable(DMA1, DMA_CH4);

    dma_deinit(DMA1, DMA_CH2);
    dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
    dma_init_struct.memory_addr = (uint32_t)(debug_rx_buff);
    dma_init(DMA1, DMA_CH2, &dma_init_struct);
    dma_circulation_disable(DMA1, DMA_CH2);
    dma_memory_to_memory_disable(DMA1, DMA_CH2);

    dma_channel_enable(DMA1, DMA_CH4);
    dma_channel_enable(DMA1, DMA_CH2);
    
    usart_dma_transmit_config(UART3, USART_TRANSMIT_DMA_ENABLE);
    usart_dma_receive_config(UART3, USART_RECEIVE_DMA_ENABLE);
}

void bsp_uart3_write_bytes(uint8_t* buff, uint8_t len)
{
    #if 1
    while(RESET == dma_flag_get(DMA1, DMA_CH4, DMA_INTF_FTFIF));
    dma_channel_disable(DMA1, DMA_CH4);
    dma_flag_clear(DMA1, DMA_CH4, DMA_FLAG_FTF);
    dma_memory_address_config(DMA1, DMA_CH4, (uint32_t)(buff));
    dma_transfer_number_config(DMA1, DMA_CH4, len);
    dma_channel_enable(DMA1, DMA_CH4);
    #else
    for(uint8_t i = 0; i < len; i++)
    {
        usart_data_transmit(UART3, buff);
        while(RESET == usart_flag_get(UART3, USART_FLAG_TBE));   
        buff++;     
    }
    #endif
}

uint8_t bsp_uart3_read_bytes(uint8_t* buff)
{
    uint8_t len = 0;
    while(RESET == dma_flag_get(DMA1, DMA_CH2, DMA_FLAG_FTF));
    len = UART3_TX_RX_SIZE - dma_transfer_number_get(DMA1, DMA_CH2);
    if(len > 0)
    {
        memmove(buff, debug_rx_buff, len);
        dma_channel_disable(DMA1, DMA_CH2);
        dma_flag_clear(DMA1, DMA_CH2, DMA_FLAG_FTF);
        dma_transfer_number_config(DMA1, DMA_CH2, UART3_TX_RX_SIZE);
        dma_channel_enable(DMA1, DMA_CH2);
    }
    return len;
}
